package com.vrv.netmgr.entry;

import java.util.ArrayList;
import java.util.List;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.vrv.common.GlobalConstants;
import com.vrv.common.UpdateDialog;
import com.vrv.common.UpdateDialog.MyDialogInt;
import com.vrv.common.util.CheckNetStatus;
import com.vrv.netmgr.EdpApplication;
import com.vrv.netmgr.R;
import com.vrv.netmgr.upgrade.CheckVersion;

/**
 * @Description 主界面
 *
 * @author gaopei
 * @date
 */
@SuppressWarnings("deprecation")
public class MainTab extends TabActivity {
    private static final String TAG = MainTab.class.getSimpleName();

    private static final boolean DEBUG = true && GlobalConstants.DEBUG;

    private TabHost tabHost;
    private LinearLayout todayInfo;
    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;

    public static final String TAB_TODAY = "tabToday";
    public static final String TAB_CONFIG = "tabConfig";
    public static final String TAB_STRATEGY = "tabStragegy";
    public static final String TAB_ASSETS = "tabAssets";
    public static final String TAB_ALARM = "tabAlarm";

    private TextView titleTxt;
    private ImageButton backBtn;
    private ImageButton moreFunctionBtn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main_tab);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_title);

        init();
        
        /** 检测更新 */
        checkUpgrade();
        
//		startService(new Intent(this, MainService.class));
    }

    /**
     * 初始化工作
     */
    private void init() {
        resetTitle();
        findViews();
        addListener();
        initTab();
        
        mCheckVersion = new CheckVersion(MainTab.this, mHandler);
    }

    /**
     * 重置标题
     */
    public void resetTitle() {
        setTitle(this.getString(R.string.todayInfo));
        backBtn = (ImageButton) findViewById(R.id.backBtn);
        backBtn.setVisibility(View.GONE);
    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        if (titleTxt == null) {
            titleTxt = (TextView) findViewById(R.id.title_text);
        }
        titleTxt.setText(title);
    }

    /**
     * 设置标题；可根据showBtn参数来决定是否显示标题栏的返回按钮
     *
     */
    public void setTitle(String title, boolean showBtn) {

        if (titleTxt == null) {
            titleTxt = (TextView) findViewById(R.id.title_text);
        }
        titleTxt.setText(title);
        backBtn = (ImageButton) findViewById(R.id.backBtn);
        if (showBtn)
            backBtn.setVisibility(View.VISIBLE);
        else backBtn.setVisibility(View.GONE);
    }

    /**
     * 查找控件
     */
    private void findViews() {
        todayInfo = (LinearLayout) this.findViewById(R.id.main_todayInfo);
        linearLayout1 = (LinearLayout) this.findViewById(R.id.main_layout1);
        linearLayout2 = (LinearLayout) this.findViewById(R.id.main_layout2);
        linearLayout3 = (LinearLayout) this.findViewById(R.id.main_layout3);
        linearLayout4 = (LinearLayout) this.findViewById(R.id.main_layout4);
        //
        textView1 = (TextView) this.findViewById(R.id.main_tv1);
        textView2 = (TextView) this.findViewById(R.id.main_tv2);
        textView3 = (TextView) this.findViewById(R.id.main_tv3);
        textView4 = (TextView) this.findViewById(R.id.main_tv4);
        textView5 = (TextView) this.findViewById(R.id.main_tv5);
    }

    /**
     * 添加监听器
     */
    private void addListener() {
        todayInfo.setOnClickListener(labelClickListener);
        linearLayout1.setOnClickListener(labelClickListener);
        linearLayout2.setOnClickListener(labelClickListener);
        linearLayout3.setOnClickListener(labelClickListener);
        linearLayout4.setOnClickListener(labelClickListener);
    }

    /**
     * 点击Tab事件
     */
    OnClickListener labelClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            String curTabTag = tabHost.getCurrentTabTag();
            setLinearLayoutBackground(v.getId());
            switch (v.getId()) {
            case R.id.main_todayInfo:
                if (!curTabTag.equals(TAB_TODAY)){
                    tabHost.setCurrentTabByTag(TAB_TODAY);
                    resetTitle();
                }
                break;
            case R.id.main_layout1:
                if (!curTabTag.equals(TAB_CONFIG)) {
                    tabHost.setCurrentTabByTag(TAB_CONFIG);
                    setTitle(MainTab.this.getString(R.string.configuration));
                }
                break;
            case R.id.main_layout2:
                if (!curTabTag.equals(TAB_STRATEGY)) {
                    tabHost.setCurrentTabByTag(TAB_STRATEGY);
                    setTitle(MainTab.this.getString(R.string.strategy));
                }
                break;
            case R.id.main_layout3:
                if (!curTabTag.equals(TAB_ASSETS)) {
                    tabHost.setCurrentTabByTag(TAB_ASSETS);
                    setTitle(MainTab.this.getString(R.string.assets));
                }
                break;
            case R.id.main_layout4:
                if (!curTabTag.equals(TAB_ALARM)) {
                    tabHost.setCurrentTabByTag(TAB_ALARM);
                    setTitle(MainTab.this.getString(R.string.alarm));
                }
                break;
            }
        }
    };

    /**
     * 设置TextView的背景
     *
     * @param id
     */
    private void setLinearLayoutBackground(int id) {

        List<LinearLayout> linearLayoutList = new ArrayList<LinearLayout>();
        linearLayoutList.add(todayInfo);
        linearLayoutList.add(linearLayout1);
        linearLayoutList.add(linearLayout2);
        linearLayoutList.add(linearLayout3);
        linearLayoutList.add(linearLayout4);
        for (LinearLayout linearLayout : linearLayoutList) {
            if (linearLayout.getId() == id) {
                linearLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
                //
                switch(linearLayout.getId()){
                    case R.id.main_todayInfo:
                        textView1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_todayinfo_selected, 0, 0);
                        textView2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_config, 0, 0);
                        textView3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_strategy, 0, 0);
                        textView4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_assets, 0, 0);
                        textView5.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_alarm, 0, 0);
                        break;
                    case R.id.main_layout1:
                        textView2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_config_selected, 0, 0);
                        textView1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_todayinfo, 0, 0);
                        textView3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_strategy, 0, 0);
                        textView4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_assets, 0, 0);
                        textView5.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_alarm, 0, 0);
                        break;
                    case R.id.main_layout2:
                        textView3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_strategy_selected, 0, 0);
                        textView1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_todayinfo, 0, 0);
                        textView2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_config, 0, 0);
                        textView4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_assets, 0, 0);
                        textView5.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_alarm, 0, 0);
                        break;
                    case R.id.main_layout3:
                        textView4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_assets_selected, 0, 0);
                        textView1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_todayinfo, 0, 0);
                        textView2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_config, 0, 0);
                        textView3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_strategy, 0, 0);
                        textView5.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_alarm, 0, 0);
                        break;
                    case R.id.main_layout4:
                        textView5.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_alarm_selected, 0, 0);
                        textView1.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_todayinfo, 0, 0);
                        textView2.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_config, 0, 0);
                        textView3.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_strategy, 0, 0);
                        textView4.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.png_assets, 0, 0);
                        break;
                    }
            } else {
                linearLayout.setBackgroundDrawable(null);
            }
        }
    }

    /**
     * 初始化标签页
     */
    private void initTab() {
        tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec(TAB_TODAY).setIndicator(TAB_TODAY).setContent(new Intent(this, TodayInfoActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(TAB_CONFIG).setIndicator(TAB_CONFIG).setContent(new Intent(this, ConfigActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(TAB_STRATEGY).setIndicator(TAB_STRATEGY).setContent(new Intent(this, StrategyActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(TAB_ASSETS).setIndicator(TAB_ASSETS).setContent(new Intent(this, AssetsActivity.class)));
        tabHost.addTab(tabHost.newTabSpec(TAB_ALARM).setIndicator(TAB_ALARM).setContent(new Intent(this, AlarmActivity.class)));
//		tabHost.setCurrentTabByTag(TAB_TODAY);
    }

    public ImageButton getBackBtn() {
        return backBtn;
    }

    public ImageButton getMoreFuncBtn() {
        return moreFunctionBtn;
    }

    
    /** 版本检测处理类 */
    public CheckVersion mCheckVersion;

    /**
     * 检测更新最新版本
     */
    public void checkUpgrade() {
        if (DEBUG) Log.v(TAG, "checkUpgrade()");
        
        new Thread() {
            public void run() {
                if (DEBUG) Log.v(TAG, "checkUpgrade()...Thread()...run()");
                
                //探测wifi
                boolean bWifi = CheckNetStatus.detectWifi();
                if (!bWifi) {
                    return;
                }
                
                //探测服务器
                boolean bServer = CheckNetStatus.detectServer();
                if (!bServer) {
                    return;
                }
                
                //更新网络状态
                EdpApplication.setNetStatus(bWifi & bServer);
                
                int local = mCheckVersion.getLocalVersionCode();
                int remote = mCheckVersion.getRemoteVersionCode();

                if (remote < 0) {
                    return;
                }

                boolean b = mCheckVersion.compareVersion(local, remote);
                if (b) {
                    Message msg = new Message();
                    msg.what = UPDATE_SELECT;
                    mHandler.sendMessage(msg);
                }
            }
        }.start();
    }

    /**
     * 选择升级或取消
     */
    public void doSelect() {
        if (DEBUG)
            Log.v(TAG, "doSelect()");
        //
        final UpdateDialog ud = new UpdateDialog(MainTab.this);
        ud.setLeftButton("确定", new MyDialogInt() {
            @Override
            public void onClick(View view) {
                ud.dismiss();
                String file = mCheckVersion.getApkName();
                mCheckVersion.download(file, null);
            }
        });
        ud.setRightButton("取消", new MyDialogInt() {
            @Override
            public void onClick(View view) {
                ud.dismiss();
            }
        });
    }

    /**
     * 消息定义
     */
    public final int UPDATE_SELECT = 0x100;
    /**
     * 消息处理
     */
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_SELECT:
                doSelect();
            default:
                break;
            }
        }
    };
}